package com.hdax.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.hdax.gulimall.product.entity.ProductAttrValueEntity;
import com.hdax.gulimall.product.service.ProductAttrValueService;
import com.hdax.gulimall.product.vo.AttrResponseVo;
import com.hdax.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hdax.gulimall.product.entity.PmsAttrEntity;
import com.hdax.gulimall.product.service.PmsAttrService;
import com.hdax.common.utils.PageUtils;
import com.hdax.common.utils.R;
import org.w3c.dom.Attr;


/**
 * 商品属性
 *
 * @author xiaohou
 * @email s2687781461@qq.com
 * @date 2023-04-07 17:41:13
 */
@RestController
@RequestMapping("product/attr")
public class PmsAttrController {
    @Autowired
    private PmsAttrService pmsAttrService;


    @Autowired
    private ProductAttrValueService productAttrValueService;

    @RequestMapping("/base/listforspu/{spuId}")
    public R listForspu(@PathVariable("spuId") Long spuId){

        List<ProductAttrValueEntity> list=productAttrValueService.attrListforspu(spuId);

        return R.ok().put("data",list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:pmsattr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = pmsAttrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询所有的分组属性,and 查询销量
     * @param paramd
     * @param catelogId
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String,Object> paramd,@PathVariable Long catelogId,@PathVariable("attrType") String type){
        PageUtils pageUtils=pmsAttrService.queryBaseAttrList(paramd,catelogId,type);
        return R.ok().put("page",pageUtils);
    }




    /**
     * 根据分组属性的id来修改分组属性
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:pmsattr:info")
    public R info(@PathVariable("attrId") Long attrId){
        AttrResponseVo attrResponseVo=pmsAttrService.getInfo(attrId);
        return R.ok().put("attr", attrResponseVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:pmsattr:save")
    public R save(@RequestBody AttrVo attrVo){
        //当前对象包含参数对象的所有值
		pmsAttrService.saveAttr(attrVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update/{spuId}")
    //@RequiresPermissions("product:pmsattr:update")
    public R updateSpuAttr(@PathVariable Long spuId ,@RequestBody List<ProductAttrValueEntity> entitys){
        System.out.println(spuId);
        System.out.println(entitys);
        productAttrValueService.updateSpuAttr(spuId,entitys);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:pmsattr:delete")
    public R delete(@RequestBody Long[] attrIds){
		pmsAttrService.removeDetail(Arrays.asList(attrIds));

        return R.ok();
    }


}
